<template>
  <div class="subject">
    <span>{{num1}}</span>
    <span>+</span>
    <span>{{num2}}</span>
    <span>=</span>
    <input type="number" v-model.number="sum" />
    <button @click="sub(index)">提交</button>
  </div>
</template>

<script>
export default {
  name: 'subject-com',
  props: ['index'],
  data () {
    return {
      num1: 0,
      num2: 0,
      sum: 0
    }
  },
  methods: {
    sub (index) {
      console.log(index, this.sum === this.num1 + this.num2 ? 1 : 2)
      this.$emit('sub', index, this.sum === this.num1 + this.num2 ? 1 : 2)
    }
  },
  // 实例创建后
  created () {
    this.num1 = parseInt(Math.random() * 10)
    this.num2 = parseInt(Math.random() * 10)
  }
}
</script>

<style scoped>
.subject {
  margin: 5px;
  padding: 5px;
  font-size: 20px;
}
.subject span {
  display: inline-block;
  text-align: center;
  width: 20px;
}
.subject input {
  width: 50px;
  height: 20px;
}
</style>
